package gw.com.android.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.gwtsz.gts2.hx.R;

import gw.com.android.app.GTConfig;
import gw.com.android.terminal.GTSDataListener;
import gw.com.android.ui.MainActivity;
import gw.com.android.utils.ColorThemeUtil;
import www.com.library.app.ObjectSessionStore;

/**
 * @author jett
 * @since 2017-10-18.
 */
public class SelectColorDialog implements View.OnClickListener {

    private final Context context;
    private Dialog dialog;
    private OnColorSelectedListener listener;

    public SelectColorDialog(Context context) {
        this.context = context;
        init();
    }

    public SelectColorDialog setColorListener(OnColorSelectedListener listener) {
        this.listener = listener;
        return this;
    }

    private void init() {
        View root = View.inflate(context, R.layout.dialog_select_color, null);
        initView(root);

        ViewGroup.LayoutParams contentParam = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog = new Dialog(context, R.style.dialog_no_title);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(root, contentParam);
    }

    private void initView(View root) {
        root.findViewById(R.id.green_raise).setOnClickListener(this);
        root.findViewById(R.id.red_raise).setOnClickListener(this);
        root.findViewById(R.id.tv_cancel).setOnClickListener(this);
    }

    public void show() {
        if (dialog == null) {
            init();
        }
        dialog.show();
    }

    public void dismiss() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }

    @Override
    public void onClick(View v) {
        dismiss();
        switch (v.getId()) {
            case R.id.green_raise:
                //绿涨红跌
                ColorThemeUtil.instance().DefaultColorTheme();
                GTConfig.instance().savePrefColorConfig(GTConfig.COLOR_GREEN_RISE);
                MainActivity mainActivity = (MainActivity) ObjectSessionStore.getObject(MainActivity.class.getSimpleName() + GTSDataListener.mainNum);
                if (mainActivity != null) {
                    mainActivity.removeQuoteFragment();
                }
                if (this.listener != null) {
                    listener.onSelect(GTConfig.COLOR_GREEN_RISE);
                }
                break;
            case R.id.red_raise:
                //红涨绿跌
                ColorThemeUtil.instance().changeColorTheme();
                GTConfig.instance().savePrefColorConfig(GTConfig.COLOR_RED_RISE);
                MainActivity activity = (MainActivity) ObjectSessionStore.getObject(MainActivity.class.getSimpleName() + GTSDataListener.mainNum);
                if (activity != null) {
                    activity.removeQuoteFragment();
                }
                if (this.listener != null) {
                    listener.onSelect(GTConfig.COLOR_RED_RISE);
                }
                break;
            default:
                break;
        }
    }

    public interface OnColorSelectedListener {
        void onSelect(String color);
    }

}
